package com.wangtao.firstapp.activity;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.GalleryAdapter;
import com.wangtao.firstapp.custom.MyGridView;
import com.wangtao.firstapp.modles.GalleryEntity;

import java.util.ArrayList;


import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;

/**
 * 拍照页面
 * Created by lanouhn on 16/9/3.
 */
public class TakePhotoActivity extends AppCompatActivity {
    private TextView tv_takephoto_cancel;// 取消
    private TextView tv_allphoto;// 标题TextView
    private ImageView iv_arrow_down, iv_arrow_up;// 箭头
    private ImageView iv_item_takephoto;
    private boolean isUp = true;
    private GalleryEntity galleryEntity;// 本地相册实体;
    private GalleryAdapter galleryAdapter;// 用于显示本地相册的适配器
    private ArrayList<GalleryEntity> arrayList;// 数组中存放的是单个图片的uri
    private MyGridView mygridview_pickphoto;// 全部照片下用于显示照片的GridView

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_takephoto);
        initView();
        initEvent();
        getGallery();
    }

    private void initView() {
        tv_takephoto_cancel = (TextView) findViewById(R.id.tv_takephoto_cancel);
        iv_arrow_down = (ImageView) findViewById(R.id.iv_arrow_down);
        iv_arrow_up = (ImageView) findViewById(R.id.iv_arrow_up);
        View view = LayoutInflater.from(TakePhotoActivity.this).inflate(R.layout.item_takephoto, null);
        iv_item_takephoto = (ImageView) view.findViewById(R.id.iv_item_takephoto);
        tv_allphoto = (TextView) findViewById(R.id.tv_allphoto);
        mygridview_pickphoto = (MyGridView) findViewById(R.id.mygridview_pickphoto);
    }

    private void initEvent() {
        // 点击销毁当前Activity
        tv_takephoto_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 点击"全部照片"
        tv_allphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUp) {
                    iv_arrow_down.setVisibility(View.INVISIBLE);
                    iv_arrow_up.setVisibility(View.VISIBLE);
                    isUp = false;
                } else {
                    iv_arrow_down.setVisibility(View.VISIBLE);
                    iv_arrow_up.setVisibility(View.INVISIBLE);
                    isUp = true;
                }
            }
        });
        // 点击箭头
        iv_arrow_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUp) {
                    iv_arrow_down.setVisibility(View.INVISIBLE);
                    iv_arrow_up.setVisibility(View.VISIBLE);
                    isUp = false;
                } else {
                    iv_arrow_down.setVisibility(View.VISIBLE);
                    iv_arrow_up.setVisibility(View.INVISIBLE);
                    isUp = true;
                }
            }
        });

        // 点击拍摄照片
        iv_item_takephoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_takephoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivity(intent_takephoto);
            }
        });
    }

    // 获取手机本地相册并设置适配器
    private void getGallery() {
//        ContentResolver contentResolver = getContentResolver();
//        Uri uri = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
//        String[] proj = {MediaStore.Images.Media.DATA};
//        Cursor cursor = contentResolver.query(uri, proj, null, null, null);
//        cursor.moveToFirst();
//       // while (cursor.moveToNext()) {
//            for (int i = 0; i < cursor.getCount(); i++) {
//                galleryEntity = new GalleryEntity();
//                arrayList = new ArrayList<>();
//                galleryEntity.setUri(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));
//                arrayList.add(galleryEntity);
//            }
//        //}
//        if (galleryAdapter == null) {
//            galleryAdapter = new GalleryAdapter(TakePhotoActivity.this, arrayList);
//            mygridview_pickphoto.setAdapter(galleryAdapter);
//        } else {
//            galleryAdapter.notifyDataSetChanged();
//        }

        PhotoPickerIntent intent_pick = new PhotoPickerIntent(TakePhotoActivity.this);
        intent_pick.setPhotoCount(9);
        intent_pick.setShowCamera(true);
        startActivityForResult(intent_pick, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == 1) {
            if (data != null) {
                ArrayList<String> photos =
                        data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
            }
        }
    }
}
